package org.rest
import grails.converters.JSON
import grails.converters.XML

class SuppliersController {
	
	def list = { 
		response.setHeader("Content-Location",g.createLink(controller:"suppliers",absolute:"true") as String)
		def suppliers = Suppliers.list()
		if (suppliers.isEmpty()){
			response.status = 204
			render ""
		} else {
			response.status = 200
			withFormat {
				json { getJSON(suppliers)  }
			}
		}
	}
	
	def show = {
		def suppliers = Suppliers.get(params.id)
		if (suppliers) {
			response.setHeader("Content-Location",g.createLink(controller:"suppliers",id:params.id,absolute:"true") as String)
			withFormat {
				json { render getSupplierJSON(suppliers) as JSON }
			}
		} else {
			sendNotFoundResponse()
		}
	}	
	
	def create = {
		def suppliers = new Suppliers(params["supplier"])
		if (suppliers.validate()) {
			suppliers.save();
			response.setHeader("Content-Location",g.createLink(controller:"suppliers",id:suppliers.id,absolute:"true") as String)
			response.status = 201
			withFormat {
				json { render getSupplierJSON(suppliers) as JSON }
			}
		} else {
			sendErrorResponse(suppliers, 403)
		}
	} 	
	
	def delete = {
		def  suppliers = Suppliers.get(params.id)
		if (!suppliers) {
			sendNotFoundResponse()
		}
		suppliers.delete();
		response.status = 204
		render ""
	}
	
	def update = {
		def suppliers = Suppliers.get(params.id)
		if (!suppliers) {
			sendNotFoundResponse()
		}
		bindData(suppliers, params["supplier"])
		if (suppliers.validate()) {
			suppliers.save(flush:true);
			response.setHeader("Content-Location",g.createLink(controller:"suppliers",id:suppliers.id,absolute:"true") as String)
			response.status = 200
			withFormat {
				json { render getSupplierJSON(suppliers) as JSON }
			}
		} else {
			sendErrorResponse(suppliers, 403)
		}
	} 
	
	
	
	def getJSON = { suppliers ->
		def newList = new ArrayList()
		suppliers.each { s ->
			newList.add(getSupplierJSON(s))
		}
		def output = [
				suppliers: newList
				]
		render output as JSON
	 }
	
	
	def getSupplierJSON = { s ->
		def supplierMap = new HashMap()
		def productList = new ArrayList()
		supplierMap.put("name", s.name)	
		supplierMap.put("contactName", s.contactName)
		supplierMap.put("contactTitle",s.contactTitle)
		supplierMap.put("city",s.city)
		supplierMap.put("region", s.region)
		supplierMap.put("postalCode", s.postalCode)
		supplierMap.put("countrye",s.country)
		supplierMap.put("phone", s.phone)
		supplierMap.put("fax", s.fax)
		supplierMap.put("homePage", s.homePage)
		supplierMap.put("address", s.address)
		
		s.products.each { p -> 
			def productMap = new HashMap()
			productMap.put ("uri", g.createLink(controller:"product", id:p.id,absolute:"true"))
			productList.add(productMap)	
		}
		supplierMap.put("products", productList)
		def uri = g.createLink(controller:"suppliers", id:s.id,absolute:"true") 
		supplierMap.put("uri",uri)
		return supplierMap
	}
	
	
	private def sendErrorResponse(suppliers, status) {
		response.status = status
		render contentType: "application/xml", {
			errors {
				suppliers?.errors?.fieldErrors?.each {err ->
					field(err.field)
					message(g.message(error: err))
				}
			}
		}
	}
	
	
	private def sendNotFoundResponse() {
		response.status = 404
		render contentType: "application/xml", {
			errors {
				message("suppliers not found with id: " + params.id)
			}
		}
	}
	
}